/**
 * 创建对象,并初始化
 */
function User(ele, option) {
	this.initPlus()
	this.initData()
	this.initEvent();
}
User.prototype.initPlus = function() {
	$initPlus(function(web) {
		//$setText(".mui-title", web.title)
		//返回携带参数
		$backWindow('HBuilder', {
			name: "测试"
		})
	})
}
User.prototype.initData = function() {
	var imghead = mui("#imghead")[0]
	imghead.style.display = 'none'
	var emptyhead = mui("#emptyhead")[0]
	mui.plusReady(function() {
		const loginInfo = app.getState();
		if(JSON.stringify(loginInfo) === '{}'){
			return
		}
		var photo = ''//loginInfo.PerPic
		if (photo && photo!="") {
			imghead.style.display = 'block'
			imghead.src = photo
		}else{
			imghead.style.display = 'block'
			defaultImg()
		}
		emptyhead.innerText = loginInfo.loginName.slice(0,1);
		mui('#user')[0].innerText = loginInfo.loginName;
		mui('#loc')[0].innerText = loginInfo.loginWardDesc;
		imghead.addEventListener('error',function(){
			// mui("#emptyhead")[0].style.display = 'block'
			// imghead.style.display = 'none'
		})
	})
}
User.prototype.initEvent = function() {
	mui('body').on('tap','.flexHR',function(){
	  var url = this.getAttribute('data-url')
	  openWindow(url,url,{})
	}) 
	//head跳转
	// $click("header", function() {
	// 	var url = this.getAttribute('data-url')
	// 	openWindow(url,url,{})
	// })
	
	//切换科室
	$click("arrow", function() {
		var locPicker;
		var pickerJson;
		var user = app.getState()
		var wardList = user['WardList'];
		if(typeof(wardList) == 'string'){  // 后端应该输出对象，不应该是现在的字符串
			wardList = wardList.replace(/'/g,'"');
			try{
				const wards = JSON.parse(wardList);
				pickerJson = wards.map(ward => ({
					value: ward.WardId,
					text: ward.WardDesc
				}))
			}catch(e){
				mui.toast("JSON解析错误！",e)
			}
		}
		
		//切换科室的事件
		if (plus.os.name == 'iOS') {
			locPicker = new mui.xhPicker(1,'请选择','pickerId');
			locPicker.setData(pickerJson,'选择登录科室');
		}else{
			locPicker = new mui.PopPicker();
			locPicker.setData(pickerJson);
		}
		
		locPicker.show(function(selectedItem) {
			user.loginWardDesc = selectedItem[0].text;
			user.loginWardId = selectedItem[0].value;
			app.setState(user)
			// 改变头像下的loc名称
			mui('#loc')[0].innerText = selectedItem[0].text;
			// 改变首页导航的loc名称
			var index = plus.webview.getWebviewById("index");
			if (index) {
				mui.fire(index, 'reloadLocName');
			}
			// 改变排班的默认科室
			var nurSchInfo = plus.webview.getWebviewById("../WKPlatformPages/nurSch/nurSchInfo.html");
			if (nurSchInfo) {
				// nurSchInfo.evalJS('pageBackGetLocSchData()')
			}
		});
	})
	//列表跳转
	mui("li").each(function() {
		$click(this, function() {
			var title = this.querySelector('.title').innerText
			var extras={'title':title}
			var url = this.getAttribute('data-url')
			openWindow(url,url,extras)
		})
	})
}
//创建实例
new User()

function openWindow(url,id,extras){
	var webview = plus.webview.create(url, url, {
		background: '#fff',
		statusbar: {
			background: '#fff'
		}
	}, extras);
	webview.show();
}

function defaultImg() {
	if(mui.os.plus) {
		plus.io.resolveLocalFileSystemURL("_doc/head.jpg", function(entry) {
			var s = entry.fullPath + "?version=" + new Date().getTime();
			document.getElementById("imghead").src = s;
		}, function(e) {
			document.getElementById("imghead").src = '../dependence/user/images/head.jpg';
		})
	} else {
		document.getElementById("imghead").src = '../dependence/user/images/head.jpg';
	}
}